package org.niu.twopointer;

public class MaxWater {
    public long maxWater (int[] arr) {
        // write code here
        int l = 0,r = arr.length-1;
        int minx = Math.min(arr[l],arr[r]);
        long ans = 0;
        while(l < r){
            if(arr[l] < arr[r]){
                l++;
                if(arr[l] < minx){
                    ans += (minx-arr[l]);
                }else{
                    minx = Math.min(arr[l],arr[r]);
                }
            }else{
                r--;
                if(arr[r] < minx){
                    ans += (minx-arr[r]);
                }else{
                    minx = Math.min(arr[l],arr[r]);
                }
            }
        }
        return ans;
    }
}
